package top.xchch.tool.mockito.juejin;

import org.junit.Test;

import java.util.LinkedList;

import static org.mockito.Mockito.*;

/**
 * 验证准确的调用次数，最多、最少、从未等
 */
public class InvocationTimesTest04 {
    @Test
    public void testInvocationTimes() throws Exception {

        LinkedList mockedList = mock(LinkedList.class);

        //using mock
        mockedList.add("once");

        mockedList.add("twice");
        mockedList.add("twice");

        mockedList.add("three times");
        mockedList.add("three times");
        mockedList.add("three times");

        //下面两个是等价的， 默认使用times(1)
        verify(mockedList).add("once");
        verify(mockedList, times(1)).add("once");

        //验证准确的调用次数
        verify(mockedList, times(2)).add("twice");
        verify(mockedList, times(3)).add("three times");

        //从未调用过. never()是times(0)的别名
        verify(mockedList, never()).add("never happened");

        //用atLeast()/atMost()验证
        verify(mockedList, atLeastOnce()).add("three times");
        //下面这句将不能通过测试
        verify(mockedList, atLeast(2)).add("five times");
        verify(mockedList, atMost(5)).add("three times");
    }

}
